package com.lzhpo.core.service;

import com.lzhpo.core.utils.JsonResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName GlobalExceptionHandler
 * @Description: TODO
 * @Author lijin
 * @Date 2020/7/25
 * @Author 740393778@qq.com
 * @Version V1.0
 **/
@Slf4j
@ControllerAdvice //controller的增强
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class) //给controller添加异常处理，括号中指定要捕获处理哪种异常，Exception.class表示处理所有种类的异常
    @ResponseBody  //返回给浏览器显示出来
    public JsonResp handler1(Exception e){ //如果要使用异常对象，可以作为参数传入异常对象
        log.error("global error {} , {}",e,e.getMessage());
        e.printStackTrace();
        return JsonResp.fail("程序发生错误,请联系客服.");
    }

}